﻿using System;
using System.Linq;
using System.Web.Mvc;
using Telerik.Web.Mvc;
using Transport.Areas.Administrator.Models;
using Transport.Core.Models.Settings;
using Transport.Infrastructure;
using Transport.Infrastructure.Mvc;
using Transport.Infrastructure.Security;
using Transport.Services;
using Transport.Services.Infrastructure;
using Transport.Services.Settings;

namespace Transport.Areas.Administrator.Controllers
{
    [Authorize]
    public class RatesTableController : ControllerBase<IRatesTableRepository, RatesTable>
    {
        public RatesTableController(IUnitOfWork unitOfWork, IRatesTableRepository repository)
            : base(repository, unitOfWork)
        {
        }

        //
        // GET: /Administrator/RatesTable/

        public ActionResult Index()
        {
            return View();
        }

        [GridAction]
        public ActionResult GridModel(string search)
        {
            var user = Repository.Search(search).Where(p => p.MyOfficeId == WorkContext.MyOfficeId && p.CustomerId == 0).AsEnumerable();

            var gridModel = new GridModel<RatesTableListModel>
            {
                Data = user.Select(x => new RatesTableListModel
                                            {
                                                Description = x.Description,
                                                PriceColumnA = String.Format("{0:0,0}", x.PriceColumnA),
                                                PriceColumnB = String.Format("{0:0,0}", x.PriceColumnB),
                                                PriceColumnC = String.Format("{0:0,0}", x.PriceColumnC),
                                                PriceColumnD = String.Format("{0:0,0}", x.PriceColumnD),
                                                PriceColumnE = String.Format("{0:0,0}", x.PriceColumnE),
                                                RatesTableId = x.RatesTableId,
                                            })
            };
            return new JsonResult
            {
                Data = gridModel
            };
        }

        public virtual ActionResult Edit(int id)
        {
            var entity = Repository.GetById(id);
            var model = new RatesTableModel()
            {
                Description = entity.Description,
                IsExtraColumn = entity.IsExtraColumn,
                MaxWeight = entity.MaxWeight,
                MinWeight = entity.MinWeight,
                PriceColumnA = entity.PriceColumnA,
                PriceColumnB = entity.PriceColumnB,
                PriceColumnC = entity.PriceColumnC,
                PriceColumnD = entity.PriceColumnD,
                PriceColumnE = entity.PriceColumnE,
                RatesTableId = entity.RatesTableId,
                WeightOver = entity.WeightOver
            };
            return View("Edit", model);
        }

        [HttpPost, ValidateInput(false)]
        public virtual ActionResult Save(RatesTableModel ratesTableModel)
        {
            if (ratesTableModel.RatesTableId > 0)
            {
                if (!ModelState.IsValid)
                {
                    var entity = Repository.GetById(ratesTableModel.RatesTableId);
                    var model = new RatesTableModel()
                    {
                        Description = entity.Description,
                        IsExtraColumn = entity.IsExtraColumn,
                        MaxWeight = entity.MaxWeight,
                        MinWeight = entity.MinWeight,
                        PriceColumnA = entity.PriceColumnA,
                        PriceColumnB = entity.PriceColumnB,
                        PriceColumnC = entity.PriceColumnC,
                        PriceColumnD = entity.PriceColumnD,
                        PriceColumnE = entity.PriceColumnE,
                        RatesTableId = entity.RatesTableId,
                        WeightOver = entity.WeightOver
                    };
                    return View("Edit", model);
                }

                var ratesTableEntity = Repository.GetById(ratesTableModel.RatesTableId);
                if (ratesTableEntity.IsExtraColumn)
                {
                    ratesTableEntity.WeightOver = ratesTableModel.WeightOver;
                }
                else
                {
                    ratesTableEntity.MaxWeight = ratesTableModel.MaxWeight;
                    ratesTableEntity.MinWeight = ratesTableModel.MinWeight;
                }
                ratesTableEntity.Description = ratesTableModel.Description;
                ratesTableEntity.ModifiedByUserId = WorkContext.CurrentUserId;
                ratesTableEntity.ModifiedDate = DateTime.Now;
                ratesTableEntity.PriceColumnA = ratesTableModel.PriceColumnA;
                ratesTableEntity.PriceColumnB = ratesTableModel.PriceColumnB;
                ratesTableEntity.PriceColumnC = ratesTableModel.PriceColumnC;
                ratesTableEntity.PriceColumnD = ratesTableModel.PriceColumnD;
                ratesTableEntity.PriceColumnE = ratesTableModel.PriceColumnE;
                ratesTableEntity.MyOfficeId = WorkContext.MyOfficeId;
                using (UnitOfWork)
                {
                    Repository.Update(ratesTableEntity);
                }
            }

            //Save success
            this.SetSuccessNotification(string.Format("{0} đã được lưu thành công.", "Bảng giá"));
            return RedirectToAction("Index", new { area = "Administrator" });
        }

        #region Customer Ratetable

        [GridAction]
        public ActionResult GridModelInCustomerDetail(int customerId)
        {
            var user = Repository.GetAll().Where(p => p.MyOfficeId == WorkContext.MyOfficeId && p.CustomerId == customerId).AsEnumerable();

            var gridModel = new GridModel<RatesTableListModel>
            {
                Data = user.Select(x => new RatesTableListModel
                {
                    Description = x.Description,
                    PriceColumnA = String.Format("{0:0,0}", x.PriceColumnA),
                    PriceColumnB = String.Format("{0:0,0}", x.PriceColumnB),
                    PriceColumnC = String.Format("{0:0,0}", x.PriceColumnC),
                    PriceColumnD = String.Format("{0:0,0}", x.PriceColumnD),
                    PriceColumnE = String.Format("{0:0,0}", x.PriceColumnE),
                    RatesTableId = x.RatesTableId,
                })
            };
            return new JsonResult
            {
                Data = gridModel
            };
        }

        public virtual ActionResult EditForCustomer(int id)
        {
            var entity = Repository.GetById(id);
            var model = new RatesTableModel()
            {
                Description = entity.Description,
                IsExtraColumn = entity.IsExtraColumn,
                MaxWeight = entity.MaxWeight,
                MinWeight = entity.MinWeight,
                PriceColumnA = entity.PriceColumnA,
                PriceColumnB = entity.PriceColumnB,
                PriceColumnC = entity.PriceColumnC,
                PriceColumnD = entity.PriceColumnD,
                PriceColumnE = entity.PriceColumnE,
                RatesTableId = entity.RatesTableId,
                WeightOver = entity.WeightOver,
                CustomerId = entity.CustomerId
            };
            return View("EditForCustomer", model);
        }

        [HttpPost, ValidateInput(false)]
        public virtual ActionResult SaveForCustomer(RatesTableModel ratesTableModel)
        {
            if (ratesTableModel.RatesTableId > 0)
            {
                if (!ModelState.IsValid)
                {
                    var entity = Repository.GetById(ratesTableModel.RatesTableId);
                    var model = new RatesTableModel()
                    {
                        Description = entity.Description,
                        IsExtraColumn = entity.IsExtraColumn,
                        MaxWeight = entity.MaxWeight,
                        MinWeight = entity.MinWeight,
                        PriceColumnA = entity.PriceColumnA,
                        PriceColumnB = entity.PriceColumnB,
                        PriceColumnC = entity.PriceColumnC,
                        PriceColumnD = entity.PriceColumnD,
                        PriceColumnE = entity.PriceColumnE,
                        RatesTableId = entity.RatesTableId,
                        WeightOver = entity.WeightOver
                    };
                    return View("EditForCustomer", model);
                }

                var ratesTableEntity = Repository.GetById(ratesTableModel.RatesTableId);
                if (ratesTableEntity.IsExtraColumn)
                {
                    ratesTableEntity.WeightOver = ratesTableModel.WeightOver;
                }
                else
                {
                    ratesTableEntity.MaxWeight = ratesTableModel.MaxWeight;
                    ratesTableEntity.MinWeight = ratesTableModel.MinWeight;
                }
                ratesTableEntity.Description = ratesTableModel.Description;
                ratesTableEntity.ModifiedByUserId = WorkContext.CurrentUserId;
                ratesTableEntity.ModifiedDate = DateTime.Now;
                ratesTableEntity.PriceColumnA = ratesTableModel.PriceColumnA;
                ratesTableEntity.PriceColumnB = ratesTableModel.PriceColumnB;
                ratesTableEntity.PriceColumnC = ratesTableModel.PriceColumnC;
                ratesTableEntity.PriceColumnD = ratesTableModel.PriceColumnD;
                ratesTableEntity.PriceColumnE = ratesTableModel.PriceColumnE;
                ratesTableEntity.MyOfficeId = WorkContext.MyOfficeId;
                ratesTableEntity.CustomerId = ratesTableModel.CustomerId;
                using (UnitOfWork)
                {
                    Repository.Update(ratesTableEntity);
                }
            }

            //Save success
            this.SetSuccessNotification(string.Format("{0} đã được lưu thành công.", "Bảng giá"));
            return RedirectToAction("Edit", "Customer", new { id = ratesTableModel.CustomerId, area = "Administrator" });
        }

        #endregion

    }
}
